<?php

namespace addons\yunfood\model;

use addons\yunfood\library\Logic;
use addons\yunfood\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 订单商品模型
 */
class OrderItem extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_order_item';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 商品类别
    const GOODS_TYPE_FOOD = 'food';

    public static function getGoodsTypeList()
    {
        return [self::GOODS_TYPE_FOOD => __('菜品')];
    }

    public function getGoodsTypeTextAttr($value, $data)
    {
        $optionList = self::getGoodsTypeList();

        return $optionList[$data['goods_type']] ?? $data['goods_type'];
    }

    // 商品名称s
    public function getGoodsNamesAttr($value, $data)
    {
        $goodsData = $data['goods_data'] ? json_decode($data['goods_data'], true) : [];

        return Logic::getGoodsNames($goodsData);
    }

    // 退款状态
    const REFUND_STATUS_NO     = 1;
    const REFUND_STATUS_ING    = 2;
    const REFUND_STATUS_DONE   = 3;
    const REFUND_STATUS_REFUSE = 4;
    const REFUND_STATUS_FAIL   = 5;

    public static function getRefundStatusList()
    {
        return [
            self::REFUND_STATUS_NO     => __('无'),
            self::REFUND_STATUS_ING    => __('退款中'),
            self::REFUND_STATUS_DONE   => __('退款完成'),
            self::REFUND_STATUS_REFUSE => __('已拒绝'),
            self::REFUND_STATUS_FAIL   => __('退款失败'),
        ];
    }

    public function getRefundStatusTextAttr($value, $data)
    {
        $optionList = self::getRefundStatusList();

        return $optionList[$data['refund_status']] ?? $data['refund_status'];
    }

    /**
     * 设置商品item的应付金额【变动】
     *
     * @param $orderItems
     * @param $split
     * @param $origin
     */
    public static function scaleOrderItemPayment(&$orderItems, $split, $origin)
    {
        $scale = $split / $origin;
        $totalSplit = 0;
        foreach ($orderItems as &$item) {
            $tSplit = round($item['payment'] * $scale, 2, PHP_ROUND_HALF_DOWN);
            if ($tSplit) {
                $item['payment'] -= $tSplit;
            }
            $totalSplit += $tSplit;
        }
        $tmpSplit = $split - $totalSplit;
        if ($tmpSplit) {
            foreach ($orderItems as &$item) {
                if ($item['payment'] > $tmpSplit) {
                    $item['payment'] -= $tmpSplit;
                }
            }
        }
    }

    /**
     * 设置商品item的余额支付
     *
     * @param $orderItems
     * @param $split
     * @param $origin
     */
    public static function scaleOrderItemBalance(&$orderItems, $balance, $payment)
    {
        $scale = $balance / $payment;
        $tBalance = 0;
        foreach ($orderItems as &$item) {
            $item['balance'] = round($scale * $item['payment'], 2, PHP_ROUND_HALF_DOWN);
            $tBalance += $item['balance'];
        }
        $tSplit = $balance - $tBalance;
        if ($tSplit) {
            foreach ($orderItems as &$item) {
                if ($item['balance'] > $tSplit) {
                    $item['balance'] -= $tSplit;
                }
            }
        }
    }
}
